<template>
  <div>
    <van-nav-bar title="登录账号" />
    <img src="@/assets/img/login.png" class="image" />
    <van-form @submit="login">
      <van-cell-group inset>
        <van-field v-model="phone" name="手机号码" label="手机号码" placeholder="手机号码" :rules="[{ required: true, message: '请填写手机号码' }]" />
        <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码" :rules="[{ required: true, message: '请填写密码' }]" />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          立即登录
        </van-button>
      </div>
    </van-form>
    <div class="buttons">
      <a class="button" @click="forgotPassword">忘记密码？</a>
      <a class="button" @click="showRegister">注册账号</a>
    </div>
    <div class="info">千锋教育@2023-2025</div>
  </div>
</template>

<script>
import { showDialog } from 'vant';
import { mapMutations } from 'vuex';
export default {
  data() {
    return {
      phone: '13012341234',
      password: '123456',
    }
  },
  created() {
    this.phone = app.data.phone || '13012341234';
  },
  methods: {
    ...mapMutations(['setPersonal']),
    showRegister() {
      app.push('/register', { phone: this.phone });
    },
    forgotPassword() {
      app.push('/forgotPassword', { phone: this.phone });
    },
    async login() {
      const data = await post('/detail/member', {
        search: { phone: this.phone, password: this.password },
      });
      if (!data.success) {
        return showDialog({ message: '密码错误' });
      }
      this.setPersonal(data.result);
      app.replace('/');
    }
  }
}
</script>


<style scoped lang="less">
.image {
  width: 375px;
  margin-bottom: 30px;
}
.info {
  width: 100%;
  color: gray;
  margin-bottom: 40px;
  text-align: center;
  position: absolute;
  bottom: 20px;
}
.buttons {
  display: flex;
  align-content: center;
  justify-content: space-between;
  padding: 20px 40px;
}
.button {
  color: blue;
}
</style>
